/**
 * 
 */
package com.nh.orderAction;

import java.io.FileInputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.CycleDetectionStrategy;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.Restrictions;

import com.nh.orderEntity.FloorEntity;
import com.nh.orderEntity.RemakeConfigEntity;
import com.nh.orderEntity.TableLodgeEntity;
import com.nh.orderEntity.VegetablesEntity;
import com.nh.orderEntity.VegetablesTypeEntity;
import com.nh.orderEntity.XFBomEntity;
import com.nh.orderEntity.XfEntity;

/**
 * 项目名称：OrderingMiddleware
 * 
 * 类名称：UpdataAction
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-12-19 下午11:40:32
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-12-19 下午11:40:32
 * 
 * 修改备注：
 * 
 * @version
 */
public class UpdataAction extends OrderBaseAction {
	public static String filter_delete_string = "DELETE";

	private String tabelId; // 桌子id

	/**
	 * 更新桌子状态
	 */
	public void uploadFloorStateList() {
		// List<TableLodgeEntity> tableLodgeList =
		// this.getBaseService().findAll(TableLodgeEntity.class);
		List<TableLodgeEntity> tableLodgeList = this.getBaseService().findByProperty("state", 1, TableLodgeEntity.class);

		List<StateVO> tableIds = new ArrayList<StateVO>();

		for (TableLodgeEntity temp : tableLodgeList) {
			tableIds.add(new StateVO(temp.getTid2()));
		}
		writResponse(JSONSerializer.toJSON(tableIds).toString());
	}

	/**
	 * 查询订单信息 传入桌子id 返回该桌子的订单信息
	 */
	public void uploadOrderInfo() {
		Criteria criteria = this.createCriteria(XfEntity.class);
		criteria.add(Restrictions.eq("tableId", Integer.parseInt(this.getTabelId())));
		criteria.add(Restrictions.eq("accountState", false));

		List<XfEntity> list = criteria.list();
		List<XFBomEntity> bomEntityList = new ArrayList<XFBomEntity>();
		List<VegetableVO> vegetableVOList = new ArrayList<VegetableVO>();
		XfEntity xfEntity = null;
		if (list.size() > 0) {
			xfEntity = list.get(0);
			bomEntityList = this.getBaseService().findByProperty("xfid", xfEntity.getXfid(), XFBomEntity.class);

		}

		for (XFBomEntity entity : bomEntityList) {
			VegetableVO vo = new VegetableVO();
			vo.setMunCount(entity.getCount() + "");
			vo.setMunPirce(entity.getPrice() + "");
			if (StringUtils.isEmpty(entity.getRemake())) {
				vo.setMunremake(entity.getXfRemake());
			} else {
				vo.setMunremake("[" + entity.getRemake() + "]  " + entity.getXfRemake());
			}

			vo.setMunXfid(entity.getXfid() + "");
			List<VegetablesEntity> vList = this.getBaseService().findByProperty("cid", entity.getMeunId(), VegetablesEntity.class);
			if (vList.size() > 0) {
				vo.setMunName(vList.get(0).getTitle());
			}
			// 菜类别
			List<VegetablesEntity> vegetablesEntityList = this.getBaseService().findByProperty("cid", entity.getMeunId(), VegetablesEntity.class);
			if (vegetablesEntityList.size() > 0) {
				VegetablesEntity vegetablesEntity = vegetablesEntityList.get(0);
				List<VegetablesTypeEntity> vegetablesTypeEntityTempList = this.getBaseService().findByProperty("vid", vegetablesEntity.getTypeId(), VegetablesTypeEntity.class);
				if (vegetablesTypeEntityTempList.size() > 0) {
					VegetablesTypeEntity vegetablesTypeEntity = vegetablesTypeEntityTempList.get(0);
					vo.setMunTypeTitle(vegetablesTypeEntity.getTitle());
				}

			}

			vo.setPeopleCount(xfEntity.getPeopleCount() + "");
			vo.setRemake(xfEntity.getRemake());
			vegetableVOList.add(vo);
		}

		writResponse(JSONSerializer.toJSON(vegetableVOList).toString());
	}

	public void updata() {
		List<FloorEntity> floorList = this.getBaseService().findAll(FloorEntity.class);
		List<VegetablesTypeEntity> vegetablesTypeList = this.getBaseService().findAll(VegetablesTypeEntity.class);

		// --------------------------过滤delete_条件--------------------------------------
		List<VegetablesTypeEntity> temp = new ArrayList<VegetablesTypeEntity>();
		for (int i = 0; i < vegetablesTypeList.size(); i++) {
			if (!vegetablesTypeList.get(i).getTitle().contains(filter_delete_string)) {
				temp.add(vegetablesTypeList.get(i));
			}

		}
		vegetablesTypeList = temp;
		// --------------------------过滤delete_条件--------------------------------------

		try {
			for (int i = 0; i < floorList.size(); i++) {
				FloorEntity floorEntity = floorList.get(i);
				List list = this.getBaseService().findByProperty("floorName", floorEntity.getName(), TableLodgeEntity.class);
				floorList.get(i).setTableList(list);
			}

			// System.out.println(JSONSerializer.toJSON(floorList).toString());
			// System.out.println("-----------------------------------");
			// for (int i = 0; i < vegetablesTypeList.size(); i++) {
			for (int i = 0; i < vegetablesTypeList.size(); i++) {
				VegetablesTypeEntity vegetablesTypeEntity = vegetablesTypeList.get(i);
				List<VegetablesEntity> list = this.getBaseService().findByProperty("typeId", vegetablesTypeEntity.getVid(), VegetablesEntity.class);

				// --------------------------过滤delete_条件--------------------------------------
				List<VegetablesEntity> tempList = new ArrayList<VegetablesEntity>();
				for (int j = 0; j < list.size(); j++) {
					if (!list.get(j).getTitle().contains(filter_delete_string)) {
						tempList.add(list.get(j));
					}
				}
				list = tempList;
				// --------------------------过滤delete_条件--------------------------------------

				vegetablesTypeList.get(i).setVegetablesList(list);
				// System.out.println(vegetablesTypeEntity.getTitle()+
				// "= "+list.size());
			}
			// System.out.println(JSONSerializer.toJSON(floorList).toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "id", "rid", "img", "price", "typeId", "cid", "vid", "typeId" });

		ConfigEntity configEntity = new ConfigEntity();
		configEntity.setFloorList(floorList);
		configEntity.setVegetablesTypeList(vegetablesTypeList);
		configEntity.setRemakeList(getRemakeList());
		// System.out.println(JSONSerializer.toJSON(configEntity,config).toString());
		writResponse("[" + JSONSerializer.toJSON(configEntity, config).toString() + "]");

	}

	public List<String> getRemakeList() {
		List<String> list = new ArrayList<String>();
		List<RemakeConfigEntity> res = this.getBaseService().findAll(RemakeConfigEntity.class);
		for (int i = 0; i < res.size(); i++) {
			list.add(res.get(i).getRemake());
		}
		return list;
	}

	public void init() {
		for (int j = 0; j < 10; j++) {
			FloorEntity floorEntity = new FloorEntity();
			floorEntity.setCode("001" + j);
			floorEntity.setName(j + "楼");
			floorEntity.setPinyin("12312312");
			floorEntity.setTableNum("333");

			for (int i = 0; i < j + 10; i++) {
				TableLodgeEntity tableLodgeEntity = new TableLodgeEntity();
				tableLodgeEntity.setCode("000" + i);
				tableLodgeEntity.setName("0001" + i);
				tableLodgeEntity.setPeopleCount(10);
				tableLodgeEntity.setPinyin("3333");
				tableLodgeEntity.setFloorName(floorEntity.getName());
				this.getBaseService().save(tableLodgeEntity);
			}
			this.getBaseService().save(floorEntity);
		}
		for (int j = 0; j < 10; j++) {
			VegetablesTypeEntity vegetablesType = new VegetablesTypeEntity();
			vegetablesType.setCode("00" + j);
			vegetablesType.setChinese("dddd");
			vegetablesType.setTitle("分类" + j);
			this.getBaseService().save(vegetablesType);

			for (int i = 0; i < j + 10; i++) {
				VegetablesEntity vegetablesEntity = new VegetablesEntity();
				vegetablesEntity.setChinese("dddd");
				vegetablesEntity.setCode("00" + i);
				// vegetablesEntity.setPrice(10 + i);
				vegetablesEntity.setTitle("菜" + i);
				vegetablesEntity.setTypeId(vegetablesType.getId());
				FileInputStream in;
				try {
					in = new FileInputStream("D:/testImage/1.jpg");
					Blob photo = Hibernate.createBlob(in);
					// svegetablesEntity.setImage(photo);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				this.getBaseService().save(vegetablesEntity);
			}

		}

	}

	public String getTabelId() {
		return tabelId;
	}

	public void setTabelId(String tabelId) {
		this.tabelId = tabelId;
	}
}

class LongValueProcessor implements JsonValueProcessor {

	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	private Object process(Object value) {

		return value.toString();
	}
}